# 退款申请审核系统 - 服务器部署脚本
# 目标服务器: 10.1.200.169
# 部署路径: D:\Server\RefundAuditWeb

Write-Host "========================================" -ForegroundColor Green
Write-Host "退款申请审核系统 - 服务器部署" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# 配置变量
$ServerIP = "10.1.200.169"
$LocalPath = $PSScriptRoot
$RemotePath = "\\$ServerIP\D`$\Server\RefundAuditWeb"
$BackupPath = "\\$ServerIP\D`$\Server\RefundAuditWeb_Backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"

Write-Host "服务器IP: $ServerIP" -ForegroundColor Yellow
Write-Host "本地路径: $LocalPath" -ForegroundColor Yellow
Write-Host "远程路径: $RemotePath" -ForegroundColor Yellow
Write-Host ""

# 检查网络连接
Write-Host "[1/6] 检查服务器连接..." -ForegroundColor Cyan
if (Test-Connection -ComputerName $ServerIP -Count 1 -Quiet) {
    Write-Host "✓ 服务器连接正常" -ForegroundColor Green
} else {
    Write-Host "✗ 无法连接到服务器 $ServerIP" -ForegroundColor Red
    Write-Host "请检查网络连接和服务器状态" -ForegroundColor Red
    pause
    exit 1
}

# 检查远程路径访问权限
Write-Host "[2/6] 检查远程路径访问权限..." -ForegroundColor Cyan
try {
    if (Test-Path $RemotePath) {
        Write-Host "✓ 远程路径可访问" -ForegroundColor Green
        
        # 备份现有文件
        Write-Host "[3/6] 备份现有文件..." -ForegroundColor Cyan
        Copy-Item -Path $RemotePath -Destination $BackupPath -Recurse -Force
        Write-Host "✓ 备份完成: $BackupPath" -ForegroundColor Green
    } else {
        Write-Host "! 远程路径不存在，将创建新目录" -ForegroundColor Yellow
        New-Item -Path $RemotePath -ItemType Directory -Force | Out-Null
        Write-Host "✓ 远程目录已创建" -ForegroundColor Green
    }
} catch {
    Write-Host "✗ 无法访问远程路径: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "请检查网络共享权限和凭据" -ForegroundColor Red
    pause
    exit 1
}

# 清理远程目录（保留备份）
Write-Host "[4/6] 清理远程目录..." -ForegroundColor Cyan
try {
    Get-ChildItem -Path $RemotePath | Remove-Item -Recurse -Force
    Write-Host "✓ 远程目录已清理" -ForegroundColor Green
} catch {
    Write-Host "! 清理远程目录时出现警告: $($_.Exception.Message)" -ForegroundColor Yellow
}

# 复制新文件
Write-Host "[5/6] 复制部署文件..." -ForegroundColor Cyan
try {
    $FilesToCopy = @(
        "index.html",
        "api.js",
        "app.js",
        "web.config",
        "IIS部署说明.md"
    )
    
    foreach ($file in $FilesToCopy) {
        if (Test-Path (Join-Path $LocalPath $file)) {
            Copy-Item -Path (Join-Path $LocalPath $file) -Destination $RemotePath -Force
            Write-Host "  ✓ $file" -ForegroundColor Green
        } else {
            Write-Host "  ! 文件不存在: $file" -ForegroundColor Yellow
        }
    }
    
    # 复制static文件夹
    if (Test-Path (Join-Path $LocalPath "static")) {
        Copy-Item -Path (Join-Path $LocalPath "static") -Destination $RemotePath -Recurse -Force
        Write-Host "  ✓ static文件夹" -ForegroundColor Green
    }
    
    Write-Host "✓ 文件复制完成" -ForegroundColor Green
} catch {
    Write-Host "✗ 文件复制失败: $($_.Exception.Message)" -ForegroundColor Red
    pause
    exit 1
}

# 验证部署
Write-Host "[6/6] 验证部署..." -ForegroundColor Cyan
$DeployedFiles = Get-ChildItem -Path $RemotePath -Recurse | Measure-Object
Write-Host "✓ 已部署 $($DeployedFiles.Count) 个文件/文件夹" -ForegroundColor Green

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "部署完成！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "部署信息:" -ForegroundColor Yellow
Write-Host "- 服务器: $ServerIP" -ForegroundColor White
Write-Host "- 部署路径: D:\Server\RefundAuditWeb" -ForegroundColor White
Write-Host "- 端口: 8089" -ForegroundColor White
Write-Host "- API地址: http://$ServerIP:6724/api" -ForegroundColor White
Write-Host ""
Write-Host "下一步操作:" -ForegroundColor Yellow
Write-Host "1. 在服务器上打开IIS管理器" -ForegroundColor White
Write-Host "2. 创建新网站，物理路径指向 D:\Server\RefundAuditWeb" -ForegroundColor White
Write-Host "3. 设置端口为 8089" -ForegroundColor White
Write-Host "4. 配置文件夹权限（IIS_IUSRS）" -ForegroundColor White
Write-Host "5. 启动网站" -ForegroundColor White
Write-Host ""
Write-Host "访问地址: http://$ServerIP:8089" -ForegroundColor Cyan
Write-Host ""
Write-Host "详细配置步骤请参考: IIS部署说明.md" -ForegroundColor Gray
Write-Host ""
pause